<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/11/4
 * Time: 11:35
 */
class WorkAction extends BaseAction{

    function index(){
        $art=new article();
        $ajax=Req::get('ajax');
        $sear=Req::get('search');
        $startdate=Req::get('startdate');
        $enddate=Req::get('enddate');
        $pagesize=10;
        $where='';
        if($startdate)$where.=' and startdate>='.strtotime($startdate);
        if($enddate)$where.=' and startdate<='.strtotime($enddate);
        if($ajax){
            $page=Req::get('p','intval')?Req::get('p','intval'):1;
            //$list=$art->get_list('tid=1&show=10&uid='.$this->info['masterid'].'&pg='.$page);
            $sql='select id,title,startdate,ischeck from __article a left join __work_user wu on a.id=wu.wid where a.typeid=1 and wu.uid='.$this->info['masterid'].$where.' order by id desc limit '.(($page-1)*$pagesize).','.$pagesize;
            $list=db::get_all($sql);
            $result=array();
            foreach($list as $v){
                $result[]=array('date'=>date('Y-m-d',$v->startdate),'title'=>$v->title,'name'=>$this->info['nick']);
            }
            echo json_encode($result);
        }else{
            //$list=$art->get_list('tid=1&show=10&uid='.$this->info['masterid'].$where);
            $sql='select id,title,startdate,ischeck from __article a
left join __work_user wu on a.id=wu.wid where a.typeid=1 and wu.uid='.$this->info['masterid'].$where.' order by id desc limit '.$pagesize;

            $list=db::get_all($sql);
            $this->assign('list',$list);
            $this->assign('ajax',url(array('c'=>'work','ajax'=>1)));
            $this->assign('add',url(array('c'=>'work','a'=>'add')));
            $this->assign('name',$this->info['nick']);
            $this->display('work');
        }

    }
    function add(){
        if($_POST)return $this->add_do();

        $id=Req::get('id');
        if($id){
            $sql='select id,title,shorttitle,ischeck,startdate,click from __article where id='.$id;
            $res=db::get_one($sql);
            $this->assign('obj',$res);
            $this->assign('id',$id);
        }

        $this->assign('back',url(array('c'=>'work')));
        $sql='select m.* from __master m left join __masterrole mr on m.masterid=mr.masterroleid where mr.roleid='.$this->info['roleid'].' and m.masterid!='.$this->info['masterid'];
        $list=db::get_all($sql);
        $this->assign('uid',$this->info['masterid']);
        $this->assign('user_list',$list);
        $this->display('work_add');
    }
    private function add_do(){
        $a=Req::post('a');
        $data=array('status'=>'err');
        if(!$a['title']){
            $data['msg']='工作内容不能为空!';
        }else
        if(!$a['startdate']){
            $data['msg']='日期不能为空!';
        }else{
            $id=Req::get('id');
            if($id){
                $old['startdate'] = strtotime($a['startdate']);
                $old['title']=$a['title'];
                $old['click']=$a['click'];//补贴等级
                $old['shorttitle']=$a['shorttitle'];
                if(db::_update('article',$old,'id='.$id)){
                    $data = array('status' => 'ok', 'msg' => '更新成功!');
                }else {
                    $data['msg'] = '发生未知的错误,未能提交';
                }
            }else {
                $a['typeid'] = 1;
                $a['uid'] = $this->info['masterid'];
                $a['startdate'] = strtotime($a['startdate']);
                $a['tag'] = time();
                $a['writer'] = $this->info['user'];
                $user_list = Req::post('user');
                $user_list[] = $this->info['masterid'];
                if (db::_insert('article', $a)) {
                    $lastid = db::last_id();
                    if ($lastid && is_array($user_list)) {
                        foreach ($user_list as $v) {
                            db::_query('insert ignore __work_user (`wid`,`uid`) values (' . $lastid . ',' . $v . ')');
                        }
                    }
                    $data = array('status' => 'ok', 'msg' => '提交成功!');
                } else {
                    $data['msg'] = '发生未知的错误,未能提交';
                }
            }
        }

        echo json_encode($data);
    }
}